from contextlib import contextmanager

from menu import menu

from nicegui import app, ui


def logout() -> None:
    app.storage.user.clear()
    ui.navigate.to("/login")


@contextmanager
def frame(navtitle: str):
    """
    生成一个具有统一样式和行为的自定义页面框架，用于所有页面共享。

    Args:
        navtitle (str): 页面导航标题。

    Returns:
        None.

    """
    """Custom page frame to share the same styling and behavior across all pages"""
    ui.colors(primary="#93D6", secondary="#5689", accent="#111B1E", positive="#53B689")
    with ui.column().style("").classes(
        "absolute-center items-center h-screen no-wrap p-9 w-full"
    ):
        yield
    with ui.header().classes(replace="row items-center") as header:
        ui.button(on_click=lambda: left_drawer.toggle(), icon="menu").props(
            "flat color=white"
        )
        ui.label("Getting Started").classes("font-bold")

        with ui.column().style("flex-direction:row").classes(
            "absolute-right items-right"
        ):
            ui.label(f'{app.storage.user["username"]}!').classes("text-2xl")
            ui.button(on_click=logout, icon="logout").style(
                "align-self:flex-end"
            ).props("outline round")

    with ui.footer(value=False) as footer:
        ui.label("Footer")
    with ui.left_drawer().classes("bg-blue-100") as left_drawer:
        ui.label("Menu")
        with ui.column():
            menu()
    with ui.page_sticky(position="bottom-right", x_offset=20, y_offset=20):
        ui.button(on_click=footer.toggle, icon="contact_support").props("fab")
